/******************************************************************************
 * Project:		Packet Sniffer
 * Author:		Jonathan Buttner
 * Date:		1.15.2010
 * 
 * This file will hold all the execution code for any commands
 * that the program will execute.
 *****************************************************************************/
/******************************************************************************
 * Copyright (c) 1999 - 2005 NetGroup, Politecnico di Torino (Italy)
 * Copyright (c) 2005 - 2006 CACE Technologies, Davis (California)
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Politecnico di Torino, CACE Technologies 
 * nor the names of its contributors may be used to endorse or promote 
 * products derived from this software without specific prior written 
 * permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *****************************************************************************/

#ifndef COMMANDS_H_
#define COMMANDS_H_

//#include <string.h>
#include "pcap.h"
#include "Error.h"
//#include <pthread.h>

//#include "Output.h"

/******************************************************************************
 *									FUNCTION PTRS
 *****************************************************************************/
typedef Error (*exeCmd)( 
					  void *,
					  void *,
					  void * );

/******************************************************************************
 *									STRUCTS
 *****************************************************************************/

/******************************************************************************
 * struct CMD will hold information to execute a given commanded entered in
 * the console window.
 *
 * Members:
 *	name	holds the name of the command
 *	execute	is a function pointer holding the given command's function to be
 *			executed.
 *****************************************************************************/
typedef struct CMD {
	char name[80];
	exeCmd execute;
} Command;

/******************************************************************************
 * struct LISSTRUCT will hold information to be passed onto the thread
 * that will execute the listening of packets
 *
 * Members:
 *	name	holds the name of the command
 *	execute	is a function pointer holding the given command's function to be
 *			executed.
 *****************************************************************************/
typedef struct LISSTRUCT {
	pcap_if_t *device;
	int numPak;
	pcap_t **handle; 
} ListenCmd;

/******************************************************************************
 *									GLOBALS
 *****************************************************************************/
Command *Stop;
Command *Listen;
BOOL capturePak;
/******************************************************************************
 *									FUNCTIONS
 *****************************************************************************/

/******************************************************************************
 * ERR is an enumeration that defines the different errors that can be
 * returned when executing a command.
 *
 * Members:
 *	NOTOK	defines that an error has occured
 *	OK		means that the function completed correctly
 *****************************************************************************/
//enum ERR { NOTOK, OK };

/******************************************************************************
 * exeStoplis() stops any current listening of packets
 *
 * Pre:
 *	packetCap points to a packet capture, it will be passed along to
 *	pcap_breakloop
 * Post:
 *	ends any listening of packets
 * Return:
 *	returns an Error message
 *
 *****************************************************************************/
Error exeStoplis( void *, void *, void * );

static void stopLis( pcap_t *packetCap );

/******************************************************************************
 * listenPak() is the lower level function that uses the actual needed params
 *
 * Members:
 *	device is the user chosen device to connect to
 *	numPackets is the number of packets to read, -1 is an interrupterable loop
 *  handle is a pointer to a pcap_t that will return the structure so that I
 *  can end the loop if stoplis is called
 * Pre:
 *  handle points to a structure managed by the caller that enables me to
 *  stop the listening
 * Post:
 *  handle will point to the allocated structure given by pcap_open()	
 * Return:
 *	returns an Error message
 *
 *****************************************************************************/
Error exeListen( void *, void *, void * );

static Error listenPak( 
					   pcap_if_t *device,
					   int numPackets,
					   pcap_t **handle );

/******************************************************************************
 * initCmd() stops any current listening of packets
 *
 * Post:
 *	sets up the Commands array
 *****************************************************************************/
void initCmd();

/******************************************************************************
 * destCmd() destructs the allocated Commands.
 *
 * Post:
 *	the allocated memory is freed
 *
 *****************************************************************************/
void destCmd();

/******************************************************************************
 * createLisThread() will take care of creating the thread that will run
 * the loop that will listen and capture packets.
 *
 * Members:
 *	device is the user chosen device to connect to
 *	numPackets is the number of packets to read, -1 is an interrupterable loop
 *  handle is a pointer to a pcap_t that will return the structure so that I
 *  can end the loop if stoplis is called
 * Pre:
 *  handle points to a structure managed by the caller that enables me to
 *  stop the listening
 * Post:
 *  handle will point to the allocated structure given by pcap_open()	
 * Return:
 *	returns an Error message
 * 
 *****************************************************************************/
static Error createLisThread(
							pcap_if_t *device,
							int numPackets,
							pcap_t **handle );

/******************************************************************************
 * *runLisThread() will be passed to the pthread_create() function and will
 * detail what the thread actually executes.
 *
 * Members:
 *	threadId is the ID of the thread.
 * 
 *****************************************************************************/
static void *runLisThread( void *threadId );

/******************************************************************************
 * cleanupHandler() will take care of exiting the capture loop by setting
 * a flag to false
 *
 * Members:
 *	arg is any info that the handler needs to know, it is NOT USED
 * 
 *****************************************************************************/
static void cleanupHandler( void *arg );

#endif /* COMMANDS_H_ */